# 设定该项目兼容的 cmake 最小版本
cmake_minimum_required(VERSION 3.31.7)

# 设定使用的 C++ 版本
set(CMAKE_CXX_STANDARD 23)
# 如果不满足 C++ 版本要求，则报错
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 指定是否使用编译器特有的扩展（即，不同的编译器在 C++ 标准之外自行实现的、非通用的特性）
set(CMAKE_CXX_EXTENSIONS OFF)

# 工程编译优化
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-all")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wstack-protector")

# 设定项目名 ${PROJECT_NAME}
project(network_prober VERSION 1.0.1 LANGUAGES C CXX)

# 自动添加以下路径到INCLUDE_PATH中
# 当前目标的build路径 ${CMAKE_CURRENT_BINARY_DIR}
# 当前目标的CMakeLists.txt所在路径 ${CMAKE_CURRENT_SOURCE_DIR}
#set(CMAKE_INCLUDE_CURRENT_DIR ON)

message("==================== SET CMake ENV Start===================")

# 处理器架构
message("处理器架构: CMAKE_SYSTEM_PROCESSOR => ${CMAKE_SYSTEM_PROCESSOR}")
message("CMake构建类型: CMAKE_BUILD_TYPE => ${CMAKE_BUILD_TYPE}")
message("项目根目录: PROJECT_SOURCE_DIR => ${PROJECT_SOURCE_DIR}")

# 项目级include路径
set(PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
message("项目级include路径: PROJECT_INCLUDE_DIR => ${PROJECT_INCLUDE_DIR}")

#set(PROJECT_LIBRARY_INCLUDE_DIR ${PROJECT_LIBRARY_DIR}/${CMAKE_BUILD_TYPE}/include)
#message("PROJECT_LIBRARY_INCLUDE_DIR ==> ${PROJECT_LIBRARY_INCLUDE_DIR}")

#set(PROJECT_LIBRARY_LIB_DIR ${PROJECT_LIBRARY_DIR}/${CMAKE_BUILD_TYPE}/lib)
#message("PROJECT_LIBRARY_LIB_DIR ==> ${PROJECT_LIBRARY_LIB_DIR}")

#set(PROJECT_LIBRARY_DLL_DIR ${PROJECT_LIBRARY_DIR}/${CMAKE_BUILD_TYPE}/dll)
#message("PROJECT_LIBRARY_DLL_DIR ==> ${PROJECT_LIBRARY_DLL_DIR}")

# 项目插件路径 [自定义]
#set(PROJECT_PLUGINS_DIR ${PROJECT_SOURCE_DIR}/plugins/${CMAKE_BUILD_TYPE})
#message("插件目标输出路径：PROJECT_PLUGINS_DIR ==> ${PROJECT_PLUGINS_DIR}")

# 项目插件路径 [自定义]
#set(PROJECT_BINARY_PLUGINS_DIR ${PROJECT_BINARY_DIR}/plugins)
#message("插件目标使用路径：PROJECT_BINARY_PLUGINS_DIR ==> ${PROJECT_BINARY_PLUGINS_DIR}")

#message("项目插件路径[自定义]: PROJECT_PLUGINS_DIR ==> ${PROJECT_PLUGINS_DIR}")
## 项目插件路径/include [自定义]
#set(PROJECT_PLUGINS_INCLUDE_DIR ${PROJECT_PLUGINS_DIR}/${CMAKE_BUILD_TYPE}/include)
#message("项目插件路径/include[自定义]: PROJECT_PLUGINS_INCLUDE_DIR ==> ${PROJECT_PLUGINS_INCLUDE_DIR}")
## 项目插件路径/lib [自定义]
#set(PROJECT_PLUGINS_LIB_DIR ${PROJECT_PLUGINS_DIR}/${CMAKE_BUILD_TYPE}/lib)
#message("项目插件路径/lib[自定义]: PROJECT_PLUGINS_LIB_DIR ==> ${PROJECT_PLUGINS_LIB_DIR}")
## 项目插件路径/dll [自定义]
#set(PROJECT_PLUGINS_DLL_DIR ${PROJECT_PLUGINS_DIR}/${CMAKE_BUILD_TYPE}/dll)
#message("项目插件路径/dll[自定义]: PROJECT_PLUGINS_DLL_DIR ==> ${PROJECT_PLUGINS_DLL_DIR}")

## [内置]
#set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
#message("CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ==> ${CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS}")
#
## 可执行文件的存放路径[内置]
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
#message("CMAKE_RUNTIME_OUTPUT_DIRECTORY ==> ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")

# lib生成路径 默认存放静态库的位置，以及MSVC中动态库的lib文件的位置[内置]
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
#message("CMAKE_ARCHIVE_OUTPUT_DIRECTORY ==> ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")

# 存放动态库的位置[内置]
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
#message("CMAKE_LIBRARY_OUTPUT_DIRECTORY ==> ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

# pdb生成路径
#set(CMAKE_PDB_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
#message("pdb生成路径：CMAKE_PDB_OUTPUT_DIRECTORY ==> ${CMAKE_PDB_OUTPUT_DIRECTORY}")
#
## 指定项目构建所需要的库文件的路径 库名Config.cmake
#set(CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
#message("CMAKE构建PATH：CMAKE_PREFIX_PATH ==> ${CMAKE_PREFIX_PATH}")

# 引入lib64的库[openssl] sudo ldconfig /usr/local/lib64/
list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib64/cmake")

# 引入项目的cmake模块文件
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

# 为GCC编译器增加了-DTWE_DEBUG参数，表示在DEBUG模式下，增加了宏定义TWE_DEBUG
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DTWE_DEBUG")

# 阻止gcc编译器优化时使用内置版本的malloc,calloc,realloc,free函数
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free")

# 设置测试选项（默认开启测试）
option(BUILD_TESTS "构建单元测试" OFF)

message("==================== SET CMake ENV End ===================")

# 全局头文件目录(全局引入，不用public权限)
include_directories(
    /usr/include/mysql  # mysql
    ${PROJECT_INCLUDE_DIR}
)

# 全局连接库文件目录
link_directories(
    /usr/lib/x86_64-linux-gnu
)

# 向编译器添加预定义宏。它会在编译过程中将指定的宏定义添加到源代码中。
#add_compile_definitions(
#        ENABLE_ADP_HOME_PAGE_PLUGIN
#        ENABLE_ADP_SCREEN_MONITOR_PLUGIN
#        ENABLE_ADP_NETWORK_MONITOR_PLUGIN
#)

# 第三方依赖库
add_subdirectory(third_party)
# 业务模块
add_subdirectory(network_prober)

# 条件性包含测试
if (BUILD_TESTS)
    # 启动cmake测试
    enable_testing()
    # 测试模块
    add_subdirectory(tests)
endif ()


# 使用breathe生成注释文档
include(UseBreathe)
# 创建文档生成的目标
add_breathe_doc(
    TARGET_NAME generate_docs    # 执行文档生成的cmake目标
    SOURCE_DIR ${PROJECT_SOURCE_DIR}/sphinx_doxygen # rst文件目录
    BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/sphinx_doxygen/sphinx_build    # sphinx构建路径
    CACHE_DIR ${CMAKE_CURRENT_BINARY_DIR}/sphinx_doxygen/sphinx_doctrees # 中间缓存信息
    HTML_DIR ${CMAKE_CURRENT_BINARY_DIR}/sphinx_doxygen/docs   # html格式文档输出路径
    DOXY_FILE ${PROJECT_SOURCE_DIR}/sphinx_doxygen/Doxyfile.in  # Doxyfile模板路径
    CONF_FILE ${PROJECT_SOURCE_DIR}/sphinx_doxygen/conf.py.in   # conf.py模板路径
    COMMENT "通过Sphinx生成HTML格式文档.."    # 注释说明
)







